﻿// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// See License.txt in the project root for license information.
using System;
using System.Cloud.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace GroceryList
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private static ObservableCloudList<GroceryItem> _groceries;

        public static ObservableCloudList<GroceryItem> Groceries
        {
            get { return _groceries; }
        }

        // Run this on the UI thread.  No need to block for completion though.
        public static void InitializeGroceriesAsync(ItemsControl controlToBindTo)
        {
            // Use ObservableCloudList (similar to ObservableCollection) which implements INotifyCollectionChanged, so UI updates itself.
            SynchronizationContext uiThreadSyncContext = SynchronizationContext.Current;
            try
            {
                // ObservableCloudList<T> needs the UI thread's synchronization context to respond to changes from other clients appropriately.
                // Note for Azure deployments, we must set useGateway to true.  Local deployments do not need a gateway and generally don't have one.
                _groceries = new ObservableCloudList<GroceryItem>("localhost", 19000, "Family Grocery List", uiThreadSyncContext);
                //_groceries = new ObservableCloudList<GroceryItem>("fabricdeployerdev.cloudapp.net:19000", "Family Grocery List", uiThreadSyncContext, true);

                if (_groceries.Count == 0)
                {
                    AddDefaultItems();
                }
            }
            catch (SocketException e)
            {
                // error handling
                Contract.Assert(false, e.Message);
            }
            catch (TimeoutException e)
            {
                MessageBox.Show(String.Format("Timed out when contacting the server for a CloudList. {0}", e.Message), "GroceryList timed out connecting to server");
            }

            // Now do the data binding.  Note we should still be on the UI thread.
            if (_groceries != null)
                controlToBindTo.ItemsSource = _groceries;
        }

        public static void AddDefaultItems()
        {
            // Note - if we use AddAsync here, consider blocking until finished on each operation if we want to preserve order.
            _groceries.Add(new GroceryItem("Milk", 1));
            _groceries.Add(new GroceryItem("Cereal", 2));
            _groceries.Add(new GroceryItem("Soda - 24 pack", 2));
            _groceries.Add(new GroceryItem("The Macallan - 15 year", 1));
        }
    }
}
